package com.hushan.elevator.controller;

import com.hushan.elevator.model.DeviceInfo;
import com.hushan.elevator.service.DeviceInfoService;
import com.hushan.elevator.service.DeviceLogService;
import com.hushan.elevator.service.RiskRecordService;
import com.hushan.elevator.utils.Result;
import com.hushan.elevator.utils.ResultCode;
import com.hushan.elevator.vo.LogInfoVo;
import com.hushan.elevator.vo.RiskSearchVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/log")
public class DeviceLogController {
    private final DeviceLogService deviceLogService;
    private final DeviceInfoService deviceInfoService;

    public DeviceLogController(DeviceLogService deviceLogService, DeviceInfoService deviceInfoService) {
        this.deviceLogService = deviceLogService;
        this.deviceInfoService = deviceInfoService;
    }

    @Operation(summary = "获取全部日志", description = "获取全部日志")
    @GetMapping("")
    public Result getRisks(@PageableDefault(size = 10, sort = "created_time", direction = Sort.Direction.DESC) Pageable pageable) {
        Page<LogInfoVo> rst = deviceLogService.findAllDeviceLogs(pageable);
        return new Result(ResultCode.SUCCESS, rst);
    }

    @Operation(summary = "获取设备信息的日志信息",
            description = "")
    @GetMapping("/devi/{id}")
    public Result getLogsByDeviceId(@PathVariable("id") int id,@PageableDefault(size = 10, sort = "created_time", direction = Sort.Direction.DESC) Pageable pageable) {
        boolean detected = deviceInfoService.detectAuthorize(id);
        DeviceInfo deviceInfo = deviceInfoService.findById(id);
        if (!detected) {
            return Result.ERROR();
        }
        Page<LogInfoVo> logs = deviceLogService.findDeviceLogsByDeviceId(deviceInfo.getSerialNumber(),pageable);
        return new Result(ResultCode.SUCCESS, logs);
    }


}
